package com.qf.dao.impl;

import com.qf.dao.GoodsTypeDao;
import com.qf.domain.GoodsType;
import com.qf.utils.DataSourceUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ColumnListHandler;

import java.sql.SQLException;
import java.util.List;

public class GoodsTypeDaoImpl implements GoodsTypeDao {
    private QueryRunner queryRunner=new QueryRunner(DataSourceUtil.getDataSource());
    @Override
    public List<GoodsType> findByLevel(int i) {
        String sql=" SELECT `id`,`name`,`level`,`parent` FROM `tb_goods_type` WHERE `level`= ? ";
        try {
            return queryRunner.query(sql,new BeanListHandler<>(GoodsType.class),i);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public GoodsType findByTypeid(Integer typeId) {
        String sql=" SELECT `id`,`name`,`level`,`parent` FROM `tb_goods_type` WHERE `id`= ? ";
        try {
            return queryRunner.query(sql,new BeanHandler<>(GoodsType.class), typeId);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<String> selectName() {
        String sql=" SELECT `name` FROM `tb_goods_type` WHERE `level`=1; ";
        try {
            List<String> query = queryRunner.query(sql, new ColumnListHandler<>());
            return query;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<GoodsType> selectGoodsType(String where, List<Object> params) {
        String sql=" SELECT `id`,`name`,`level`,`parent` FROM `tb_goods_type`  "+where;
        try {
            return queryRunner.query(sql,new BeanListHandler<>(GoodsType.class),params.toArray());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void modify(int id, String name, int level, int parent) {
        String sql=" UPDATE `tb_goods_type` SET `name`=?,`level`=?,`parent`=? WHERE `id`=? ";

        try {
            queryRunner.update(sql,name,level,parent,id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public GoodsType find(String id) {
        String sql=" SELECT * FROM `tb_goods_type` WHERE `id`=?; ";
        GoodsType goodsType=null;
        try {
            goodsType = queryRunner.query(sql, new BeanHandler<>(GoodsType.class), id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return goodsType;
    }

    @Override
    public void addType(GoodsType goodsType) {
        String sql=" INSERT INTO `tb_goods_type` (`name`,`level`,`parent`) VALUES (?,?,?) ";
        Object[] params={goodsType.getName(),goodsType.getLevel(),goodsType.getParent()};
        try {
            queryRunner.update(sql,params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void removeByTId(String id) {
        String sql=" DELETE FROM `tb_goods_type` WHERE `id`=? ";
        try {
            queryRunner.update(sql,id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
